package com.leetcode;

import java.util.Arrays;
import java.util.Comparator;

public class lc5608 {

    public int minimumEffort(int[][] tasks) {
        Arrays.sort(tasks, (o1,o2)->{
            int res = Math.abs(o1[0] - o1[1]) - Math.abs(o2[0] - o2[1]);
            if(res == 0){
                return o1[0] - o2[0];
            }else{
                return -res;
            }
        });
        int ans = 0;
        int curr = 0;
        for (int[] task : tasks) {
            if (task[1] > curr) {
                int div = task[1] - curr;
                curr += div;
                ans += div;
            }
            if (task[0] > curr) {
                int dif = task[0] - curr;
                curr += dif;
                ans += dif;
            }
            curr -= task[0];
        }
        return ans;
    }

    public static void main(String[] args) {
        int[][][] ans = {
                {{1,3},{2,4},{10,11},{10,12},{8,9}},
                {{1,2},{2,4},{4,8}},



                {{1,7},{2,8},{3,9},{4,10},{5,11},{6,12}}
        };
        lc5608 s = new lc5608();
        for(int[][] a : ans){
            int res = s.minimumEffort(a);
            System.out.println(res);
        }
    }
}